package utils;

import android.util.Log;

/**
 * Created by admin on 2017/11/30.
 */
public class DigitalTrans {

    /**
     * 16进制表示的字符串转换为字节数组
     *
     * @param s 16进制表示的字符串
     * @return byte[] 字节数组
     */
    public static byte[] hexStringToByteArray(String s) {
        String replace = s.replace(" ", "");
        int len = replace.length();
        byte[] b = new byte[len / 2];
        for (int i = 0; i < len; i += 2) {
            // 两位一组，表示一个字节,把这样表示的16进制字符串，还原成一个字节
            if (i + 1 < len) {
                b[i / 2] = (byte) ((Character.digit(replace.charAt(i), 16) << 4) + Character
                        .digit(replace.charAt(i + 1), 16));
            }

        }
        return b;
    }


    /**
     * 字节数组转成16进制表示格式的字符串
     *
     * @param byteArray 要转换的字节数组
     * @return 16进制表示格式的字符串
     **/
    public static String toHexString(byte[] byteArray) {
        if (byteArray == null || byteArray.length < 1)
            throw new IllegalArgumentException("this byteArray must not be null or empty");
        final StringBuilder hexString = new StringBuilder();
        for (int i = 0; i < byteArray.length; i++) {
            if ((byteArray[i] & 0xFF) < 0x10) {//0~F前面没有零,加上0,使其格式统一
                hexString.append("0");
            }
            hexString.append(Integer.toHexString(0xFF & byteArray[i]));
        }
        return hexString.toString().toUpperCase();
    }

    //转换单个字节
    public static String toHexStringByte(byte bytes) {
        StringBuilder hexString = new StringBuilder();
        if ((bytes & 0xFF) < 0x10) {//0~F前面没有零,加上0,使其格式统一
            hexString.append("0");
        }
        hexString.append(Integer.toHexString(0xFF & bytes));
        return hexString.toString().toUpperCase();


    }

    public static String binaryToHex(String string, int length) {
        StringBuilder sb = new StringBuilder();
        sb.append(Integer.toHexString(Integer.parseInt(string)));
        int length1 = sb.length();
        if (length - length1 > 0) {
            for (int i = 0; i < length - length1; i++) {
                sb.insert(0, "0");
            }
        }
        return sb.toString().toUpperCase();
    }

    public static String integerToHex(int id, int length) {
        StringBuilder sb = new StringBuilder();
        sb.append(Integer.toHexString(id));
        int length1 = sb.length();
        if (length - length1 > 0) {
            for (int i = 0; i < length - length1; i++) {
                sb.insert(0, "0");
            }
        }
        return sb.toString().toUpperCase();
    }

    public static String integerToint(int id, int length) {
        StringBuilder sb = new StringBuilder();
        sb.append(id);
        int length1 = sb.length();
        if (length - length1 > 0) {
            for (int i = 0; i < length - length1; i++) {
                sb.insert(0, "0");
            }
        }
        return sb.toString();
    }

    //将十进制的int转为16进制字符串
    public static String hexNumberFormat(int data, int length) {
        StringBuilder sb = new StringBuilder();
        sb.append(Integer.toHexString(data));
        int length1 = sb.length();
        if (length - length1 > 0) {
            for (int i = 0; i < length - length1; i++) {
                sb.insert(0, "0");
            }
        }
        return sb.toString().toUpperCase();
    }
    //字节异或(用于指纹的校验),输入第二到第六个字节的字符串,返回一串完整的命令字符串
    public static String getFingerprintCommand(String command) {
        byte[] bytes = DigitalTrans.hexStringToByteArray(command);
        int m = 0;
        for (int i = 0; i < bytes.length; i++) {
            m = (byte) (m ^ bytes[i]);
        }
//        String s = hexNumberFormat(m, 2);
        String s = Integer.toHexString(m).toUpperCase();
        int length = s.length();
        if (length < 2) {
            for (int i = 0; i < 2 - length; i++) {
                s = "0" + s;
            }
        } else {
            s = s.substring(s.length() - 2, s.length());
        }
        return "F5" + command + s + "F5";
    }
}
